package com.example.graduatetest.view.model;

import android.app.Activity;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.widget.Toolbar;
import androidx.lifecycle.MediatorLiveData;
import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.ViewModel;

import com.example.graduatetest.R;
import com.example.graduatetest.bean.Teacher;
import com.example.graduatetest.callback.TeacherCallBack;
import com.example.graduatetest.databinding.ActivityTeacherPersonBinding;
import com.example.graduatetest.model.TeacherModel;

public class TeacherPersonViewModel extends ViewModel {
    private Activity activity;
    private ActivityTeacherPersonBinding binding;
    private String teacher_id;
    private Teacher ownTeacher;

    private Handler handler = new Handler();

    public void init(Activity activity,ActivityTeacherPersonBinding binding,String teacher_id){
        this.activity = activity;
        this.binding = binding;
        this.teacher_id = teacher_id;
        toolbarInit();
        initGetTeacher();

    }

    //初始toolbar
    private void toolbarInit(){
        Toolbar toolbar = binding.toolbar10;
        toolbar.setTitle("老师介绍");
        toolbar.setNavigationIcon(R.drawable.ic_arrow_back_black_24dp);
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                activity.onBackPressed();
            }
        });

    }

    //更新UI
    private void initMutable(Teacher teacher){

        binding.tvNameFirst.setText(teacher.getName().substring(0, 1));
        binding.tvNameAll.setText(teacher.getName());
        binding.tvPhone.setText(teacher.getPhone());
        binding.tvInfo.setText(teacher.getIntroduce());
        binding.tvTeacherNumber.setText(teacher.getNumber());
    }

    //获取Teacher对象
    private void initGetTeacher(){
        TeacherModel.selectOneTeacher(teacher_id, new TeacherCallBack() {
            @Override
            public void onSuccess(Teacher teacher) {
                ownTeacher = teacher;
                handler.post(new Runnable() {
                    @Override
                    public void run() {
                        Log.e("haha", "获取老师信息成功");
                        initMutable(ownTeacher);
                    }
                });
            }

            @Override
            public void onFailed() {
                handler.post(new Runnable() {
                    @Override
                    public void run() {
                        Toast.makeText(activity, "获取老师信息失败", Toast.LENGTH_SHORT).show();
                    }
                });
            }
        });
    }


}
